package com.imooc.controller;

import com.imooc.enums.ErrorMsg;
import com.imooc.pojo.Users;
import com.imooc.pojo.bo.UserBo;
import com.imooc.service.UserService;
import com.imooc.utils.CommonJsonResult;
import com.imooc.utils.CookieUtils;
import com.imooc.utils.JsonUtils;
import com.imooc.utils.MD5Utils;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: 逸尘风清
 * @Date: 2021/5/6
 * @Time: 22:29
 * 描述：
 */
@Api(value = "注册登录" , tags = {"注册登录相关接口"})
@RestController
@RequestMapping("passport")
public class PassportController {
    @Resource
    private UserService userService;

    @ApiOperation(value = "查询用户名是否存在", notes = "用户名是否存在")
    @GetMapping("/usernameIsExist")
    public CommonJsonResult usernameIsExist(@RequestParam @ApiParam(value = "用户名", required = true)  String username){
        if(StringUtils.isBlank(username)){
            return CommonJsonResult.errorMsg("用户名不能为空");
        }
        boolean isExist = userService.queryUsernameIsExist(username);
        if (isExist) {
            return CommonJsonResult.errorMsg("用户名已存在");
        }
        return CommonJsonResult.ok();
    }

    @ApiOperation(value = "用户注册", notes = "注册用户")
    @PostMapping("/regist")
    public CommonJsonResult register(@RequestBody UserBo userBo, HttpServletRequest request, HttpServletResponse response) {
        String username = userBo.getUsername();
        String password = userBo.getPassword();
        String confirmPassword = userBo.getConfirmPassword();
        if(StringUtils.isBlank(username) || StringUtils.isBlank(password) || StringUtils.isBlank(confirmPassword)){
            return CommonJsonResult.errorMsg(ErrorMsg.NULL_USERNAME_OR_PASSWORD);
        }
        boolean isExist = userService.queryUsernameIsExist(username);
        if (isExist) {
            return CommonJsonResult.errorMsg(ErrorMsg.USERNAME_IS_EXIST);
        }
        if(password.length() < 6){
            return CommonJsonResult.errorMsg(ErrorMsg.ERROR_PASSWORD);
        }
        if(!password.equals(confirmPassword)){
            return CommonJsonResult.errorMsg(ErrorMsg.ERROR_TWO_PASSWORD);
        }
        Users user = userService.createUser(userBo);

        Users userResult = setNullProperty(user);
        // 添加到cookie
        CookieUtils.setCookie(request, response, "user", JsonUtils.objectToJson(userResult),true);
        return CommonJsonResult.ok(userResult);
    }

    @ApiOperation(value = "用户登录", notes = "用户登录")
    @PostMapping("/login")
    public CommonJsonResult login(@RequestBody UserBo userBo, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = userBo.getUsername();
        String password = userBo.getPassword();
        if(StringUtils.isBlank(username) || StringUtils.isBlank(password)){
            return CommonJsonResult.errorMsg(ErrorMsg.NULL_USERNAME_OR_PASSWORD);
        }

        String md5Password= MD5Utils.getMD5Str(password);
        Users users = userService.queryUserForLogin(username, md5Password);
        if (users == null) {
            return CommonJsonResult.errorMsg(ErrorMsg.ERROR_USERNAME_OR_PASSWORD);
        }

        Users userResult = setNullProperty(users);
        // 添加到cookie
        CookieUtils.setCookie(request, response, "user", JsonUtils.objectToJson(userResult),true);

        return CommonJsonResult.ok(userResult);
    }

    private Users setNullProperty(Users users){
        users.setCreatedTime(null);
        users.setUpdatedTime(null);
        users.setPassword(null);
        users.setRealname(null);
        users.setEmail(null);
        users.setMobile(null);
        users.setBirthday(null);
        return users;
    }

    @ApiOperation(value = "退出登录", notes = "退出登录")
    @PostMapping("/logout")
    public CommonJsonResult logout(@RequestParam String userId, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 清除cookie
        CookieUtils.deleteCookie(request, response, "user");

        return CommonJsonResult.ok();
    }
}
